//
// Created by Nasa on 2024/5/25.
// 二分查找 找任意一个相同值的索引 没有就返回-1
//

#include <iostream>
#include <vector>

using namespace std;

int search(vector<int> l, int n)
{
    int left = 0, right = l.size() - 1, i, mid;
    while (left <= right)
    {
        mid = (left + right) / 2;
        if (l[mid] == n)
        {
            return mid;
        }
        if ( n>l[mid] )
        {
            left = mid + 1;
        } else
        {
            right = mid - 1;
        }
    }
    return -1;
}

int main()
{
    //                0    1  2  3  4  5  6  7  8  9 10 11 12  13  14  15  16
    vector<int>  v {-11, -5, 0, 0, 2, 2, 2, 2, 2, 3, 5, 7, 8, 10, 13, 70, 79};
//    vector<int> v={-1,0,3,5,9,12};
//    vector<int> v={9};
    int l = 0, r = v.size() - 1, x = 9;
    cout << search(v, x);
    return 0;
}